package com.willpower.eureka.auth;

import com.willpower.eureka.auth.response.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: wen-yi;
 * @date: 2021/10/31 16:05;
 * @Description: 登录成功后重定向
 */
//@Slf4j
//@AllArgsConstructor
@Component("myAuthenticationSuccessHandler")
public class MyAuthenticationSuccessHandler extends JSONAuthentication implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //后端一般不跳转页面 而是返回数据前端跳转
        /*
        log.info("Authentication 权限: {}",authentication.getAuthorities());
        log.info("Authentication 凭证: {}",authentication.getCredentials());
        log.info("Authentication 详情: {}",authentication.getDetails());
        log.info("Authentication 对象: {}",authentication.getPrincipal());
        */
        Result result = Result.ok().setMessage("登录成功");
        //处理编码方式，防止中文乱码
        //response.setContentType("text/json;charset=utf-8");
        //使用jackson将对象转为json
        //response.getWriter().write(new ObjectMapper().writeValueAsString(result));
        this.WriteJSON(request, response, result);
    }
}
